package com.supplychainapi.config;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import com.alibaba.druid.pool.DruidDataSourceStatLogger;
import com.alibaba.druid.pool.xa.DruidXADataSource;
import com.atomikos.jdbc.AtomikosDataSourceBean;
//import com.supplychainapi.config.datasource.SupplierDBConfig;
/*
@Configuration
@MapperScan(basePackages = "com.supplychainapi.dao.supplier", sqlSessionTemplateRef = "supplierSqlSessionTemplate")
public class SupplierMyBatisConfig {

	//@Primary //一个项目中不能同时出现
	@Bean(name = "supplierDataSource")
	public DataSource supplierDataSource(SupplierDBConfig supplierDBConfig) throws SQLException {
		
		DruidXADataSource druidXADataSource = new DruidXADataSource();
		druidXADataSource.setUrl(supplierDBConfig.getUrl());
		druidXADataSource.setUsername(supplierDBConfig.getUsername());
		druidXADataSource.setPassword(supplierDBConfig.getPassword());
		druidXADataSource.setDriverClassName(supplierDBConfig.getDriverClassName());
		druidXADataSource.setInitialSize(supplierDBConfig.getInitialSize());
		druidXADataSource.setMinIdle(supplierDBConfig.getMinIdle());
		druidXADataSource.setMaxActive(supplierDBConfig.getMaxActive());
		druidXADataSource.setMaxWait(supplierDBConfig.getMaxWait());
		druidXADataSource.setTimeBetweenEvictionRunsMillis(supplierDBConfig.getTimeBetweenEvictionRunsMillis());
		druidXADataSource.setValidationQuery(supplierDBConfig.getValidationQuery());
		druidXADataSource.setTestOnBorrow(supplierDBConfig.isTestOnBorrow());
		druidXADataSource.setTestWhileIdle(supplierDBConfig.isTestWhileIdle());
		druidXADataSource.setRemoveAbandoned(supplierDBConfig.isRemoveAbandoned());
		druidXADataSource.setRemoveAbandonedTimeout(supplierDBConfig.getRemoveAbandonedTimeout());
		
		String filters = supplierDBConfig.getFilters();
		// 配置监控统计拦截的filters不为空，设置监控信息
		if(StringUtils.isNotBlank(filters)) {
			druidXADataSource.setFilters(supplierDBConfig.getFilters());
			druidXADataSource.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000");
			// 60秒一次，保存日志到log4j2中
			druidXADataSource.setTimeBetweenLogStatsMillis(60000);
			DruidDataSourceStatLogger dataSourceStatLogger = new DruidStatLogger();
			druidXADataSource.setStatLogger(dataSourceStatLogger);
		}
		
		AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
		xaDataSource.setXaDataSource(druidXADataSource);
		xaDataSource.setUniqueResourceName("supplierDataSource");
		xaDataSource.setPoolSize(supplierDBConfig.getMinIdle());
		xaDataSource.setMinPoolSize(supplierDBConfig.getMinIdle());
		xaDataSource.setMaxPoolSize(supplierDBConfig.getMaxActive());
		xaDataSource.setBorrowConnectionTimeout(60);
		xaDataSource.setMaxIdleTime(60);
		xaDataSource.setMaintenanceInterval(60);
		xaDataSource.setTestQuery(supplierDBConfig.getValidationQuery());
		return xaDataSource;
	}

	@Bean(name = "supplierSqlSessionFactory")
	public SqlSessionFactory supplierSqlSessionFactory(@Qualifier("supplierDataSource") DataSource dataSource)
			throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/supplier/*.xml"));
		return bean.getObject();
	}

	@Bean(name = "supplierSqlSessionTemplate")
	public SqlSessionTemplate supplierSqlSessionTemplate(
			@Qualifier("supplierSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory);
	}
	
}
*/